<?php
echo $this->Session->flash();
App::Import('model', 'EvaluationAppraisal');
App::Import('model', 'EvaluationPhysiographicUnit');
App::Import('model', 'EvaluationAnnex');
$totalAreaAvaluos = 0;
$totalValorAvaluos = 0;
$totalAreaUtil = 0;
$totalAreaNoUtil = 0;
for ($j = 1; $j <= 16; $j++) {
    $totalClase[$j] = 0;
}
$valorTotalUnidadesFisiograficas = 0;
$valorTotalAnexos = 0;
?>

<div id="loading" style="display: none;">
    <?php echo $html->image('loading.gif', array('border' => "0", 'align' => 'center')); ?>
</div>

<h1>PREDIOS PROYECTO <?php echo$cod; ?></h1>
<table  id="tabla" class="tabla" >
    <thead>
        <tr>
            <th>Código</th>
            <th>Nombre</th>
            <th colspan="4"></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($properties as $prop): ?>
            <tr>
                <td><?php
        //Busco el ultimo avaluo del predio y lo sumo a la variable $totalAreaAvaluos para luego mostrar el total del avaluo del proyecto.
        $avaluos = new EvaluationAppraisal();
        $avaluos->recursive = -1;
        $avaluo = $avaluos->find('first', array('conditions' => array('EvaluationAppraisal.property_id' => $prop['Property']['id']), 'order' => array('EvaluationAppraisal.id DESC'), 'fields' => array('EvaluationAppraisal.id', 'EvaluationAppraisal.area', 'EvaluationAppraisal.valor')));
        $unidadesFisiograficas = new EvaluationPhysiographicUnit();
        $unidadesFisiograficas->recursive = 0;
        $valorTotalUnidadesFisiograficas += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.valor_unitario * EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
        $anexos = new EvaluationAnnex();
        $anexos->recursive = 0;
        $valorTotalAnexos += $anexos->field('Sum(EvaluationAnnex.magnitud * EvaluationAnnex.valor_unitario)', array('EvaluationAnnex.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id']));
        for ($i = 1; $i <= 16; $i++) {
            switch ($i) {
                case 1:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'I', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 2:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'II', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 3:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'III', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 4:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'IV', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 5:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'V', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 6:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VI', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 7:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VII', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 8:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'I', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 9:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'II', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 10:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'III', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 11:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'IV', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 12:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'V', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 13:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VI', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 14:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VII', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
                case 15:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VIII', 'EvaluationPhysiographicUnit.util' => 'SI'));
                    break;
                case 16:
                    $totalClase[$i] += $unidadesFisiograficas->field('Sum(EvaluationPhysiographicUnit.area)', array('EvaluationPhysiographicUnit.evaluation_appraisal_id' => $avaluo['EvaluationAppraisal']['id'], 'EvaluationPhysiographicUnit.clase_agrologica' => 'VIII', 'EvaluationPhysiographicUnit.util' => 'NO'));
                    break;
            }
        }
        $totalAreaAvaluos += $avaluo['EvaluationAppraisal']['area'];
        $totalValorAvaluos += $avaluo['EvaluationAppraisal']['valor'];
        //Imprimo el codigo del predio
        echo $prop['Property']['codigo']
            ?></td>
                <td><?php echo $prop['Property']['nombre'] ?></td>
                <td>
                    <br><br>
                    <?php echo $ajax->link('Avalúo', array('controller' => 'EvaluationAppraisals', 'action' => 'index', $prop['Property']['id']), array('class' => 'actions', 'update' => 'content', 'complete' => 'cargar()', 'indicator' => 'loading')); ?>
                    <br><br>
                </td>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<br>
<br>
<br>
<table border="0" id="tabla">
    <thead>
        <tr>
            <th>Clasificación agrológica</th>
            <th>Área (ha)</th>
            <th>Porcentaje %</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>I útil</td>
            <td><?php echo ($totalClase[1] > 0 and !empty($totalClase[1])) ? number_format($totalClase[1], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[1] > 0) ? number_format(($totalClase[1] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>I no útil</td>
            <td><?php echo ($totalClase[8] > 0 and !empty($totalClase[8])) ? number_format($totalClase[8], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[8] > 0) ? number_format(($totalClase[8] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>II útil</td>
            <td><?php echo ($totalClase[2] > 0 and !empty($totalClase[2])) ? number_format($totalClase[2], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[2] > 0) ? number_format(($totalClase[2] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>II no útil</td>
            <td><?php echo ($totalClase[9] > 0 and !empty($totalClase[9])) ? number_format($totalClase[9], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[9] > 0) ? number_format(($totalClase[9] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>III útil</td>
            <td><?php echo ($totalClase[3] > 0 and !empty($totalClase[3])) ? number_format($totalClase[3], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[3] > 0) ? number_format(($totalClase[3] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>III no útil</td>
            <td><?php echo ($totalClase[10] > 0 and !empty($totalClase[10])) ? number_format($totalClase[10], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[10] > 0) ? number_format(($totalClase[10] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>IV útil</td>
            <td><?php echo ($totalClase[4] > 0 and !empty($totalClase[4])) ? number_format($totalClase[4], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[4] > 0) ? number_format(($totalClase[4] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>IV no útil</td>
            <td><?php echo ($totalClase[11] > 0 and !empty($totalClase[11])) ? number_format($totalClase[11], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[11] > 0) ? number_format(($totalClase[11] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>V útil</td>
            <td><?php echo ($totalClase[5] > 0 and !empty($totalClase[5])) ? number_format($totalClase[5], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[5] > 0) ? number_format(($totalClase[5] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>V no útil</td>
            <td><?php echo ($totalClase[12] > 0 and !empty($totalClase[12])) ? number_format($totalClase[12], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[12] > 0) ? number_format(($totalClase[12] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>        </tr>
        <tr>
            <td>VI útil</td>
            <td><?php echo ($totalClase[6] > 0 and !empty($totalClase[6])) ? number_format($totalClase[6], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[6] > 0) ? number_format(($totalClase[6] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>VI no útil</td>
            <td><?php echo ($totalClase[13] > 0 and !empty($totalClase[13])) ? number_format($totalClase[13], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[13] > 0) ? number_format(($totalClase[13] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>VII útil</td>
            <td><?php echo ($totalClase[7] > 0 and !empty($totalClase[7])) ? number_format($totalClase[7], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[7] > 0) ? number_format(($totalClase[7] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>VII no útil</td>
            <td><?php echo ($totalClase[14] > 0 and !empty($totalClase[14])) ? number_format($totalClase[14], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[14] > 0) ? number_format(($totalClase[14] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>VIII útil</td>
            <td><?php echo ($totalClase[15] > 0 and !empty($totalClase[15])) ? number_format($totalClase[15], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[15] > 0) ? number_format(($totalClase[15] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
        <tr>
            <td>VIII no útil</td>
            <td><?php echo ($totalClase[16] > 0 and !empty($totalClase[16])) ? number_format($totalClase[16], 4, ',', '.') : "-" ?></td>
            <td><?php echo ($totalAreaAvaluos > 0 and $totalClase[16] > 0) ? number_format(($totalClase[16] * 100 / $totalAreaAvaluos), 2, ',', '.') : "-" ?></td>
        </tr>
    </tbody>
</table>

<table border="0" id="tabla">
    <thead>
        <tr>
            <th></th>
            <th>Área (ha)</th>
            <th>Porcentaje %</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Útil</td>
            <td><?php
        for ($i = 1; $i <= 7; $i++) {
            $totalAreaUtil += $totalClase[$i];
        }
        $totalAreaUtil += $totalClase[15];
        echo number_format($totalAreaUtil, 2, ',', '.') . " (ha)"
        ?></td>
            <td><?php echo ($totalAreaAvaluos > 0) ? number_format(($totalAreaUtil * 100 / $totalAreaAvaluos), 2, ',', '.') . " %" : "0 %" ?></td>
        </tr>
        <tr>
            <td>No útil</td>
            <td><?php
                for ($i = 8; $i <= 14; $i++) {
                    $totalAreaNoUtil += $totalClase[$i];
                }
                $totalAreaNoUtil += $totalClase[16];
                echo number_format($totalAreaNoUtil, 2, ',', '.') . " (ha)"
        ?></td>
            <td><?php echo ($totalAreaAvaluos > 0) ? number_format(($totalAreaNoUtil * 100 / $totalAreaAvaluos), 2, ',', '.') . " %" : "0 %" ?></td>
        </tr>
    </tbody>
</table>
<table class="tabla" >
    <thead>
        <tr>
            <th>Área total avaluos</th>
            <th>Área total unidades fisiograficas</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td <?php echo ((round($totalAreaAvaluos, 4) != round(($totalAreaNoUtil + $totalAreaUtil), 4)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?>><?php echo number_format($totalAreaAvaluos, 4, ',', '.') . ' (ha)' ?></td>
            <td <?php echo ((round($totalAreaAvaluos, 4) != round(($totalAreaNoUtil + $totalAreaUtil), 4)) ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?>><?php echo number_format($totalAreaNoUtil + $totalAreaUtil, 4, ',', '.') . ' (ha)' ?></td>
        </tr>
    </tbody>
</table>
<table class="tabla" >
    <thead>
        <tr>
            <th>Valor total unidades fisiograficas</th>
            <th>% unidades</th>
            <th>Valor total anexos</th>
            <th>% anexos</th>
            <th>Valor anexos + unidades fisiograficas</th>
            <th>Valor avaluos</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <?php $error = (round($totalValorAvaluos, 0) != round($valorTotalAnexos + $valorTotalUnidadesFisiograficas, 0) or ($valorTotalAnexos + $valorTotalUnidadesFisiograficas == 0)) ? true : false ?>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo "$" . number_format($valorTotalUnidadesFisiograficas, 0, ',', '.') ?></td>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo ($valorTotalAnexos + $valorTotalUnidadesFisiograficas) > 0 ? number_format(($valorTotalUnidadesFisiograficas * 100 / ($valorTotalAnexos + $valorTotalUnidadesFisiograficas)), 0, ',', '.') . "%" : "" ?></td>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo "$" . number_format($valorTotalAnexos, 0, ',', '.') ?></td>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo ($valorTotalAnexos + $valorTotalUnidadesFisiograficas) > 0 ? number_format(($valorTotalAnexos * 100 / ($valorTotalAnexos + $valorTotalUnidadesFisiograficas)), 0, ',', '.') . "%" : "" ?></td>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo "$" . number_format(($valorTotalAnexos + $valorTotalUnidadesFisiograficas), 0, ',', '.') ?></td>
            <td <?php echo ( $error ? "style='background-color: #F79F81;'" : "style='background-color: #81F7D8'") ?> ><?php echo "$" . number_format($totalValorAvaluos, 0, ',', '.') ?></td>
        </tr>
    </tbody>
</table>